package com.wt.demo;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class Demo {

    public static void main(String[] args) throws Exception {
        boolean b = deleteUploadImg("http://oos.wqun.top/img/covar/20230901185704257043ebfced4b8db51dc65d897ee719.jpg?Expires=1693570850&OSSAccessKeyId=TMP.3KfiMQv7Q7X1PxRfN79fUhan3BvRyD3BqRFwf7HXGnz32VruFVt8PtYF4j5fdZtFX5t5jftqTSG4dFr8GQuDDs7Zw3E295&Signature=vK%2BWgOc3bR8na%2BEc6iADZeErBRk%3D");
        System.out.println(b);
    }

    public static boolean deleteUploadImg(String oosImgPath){
        System.out.println("图片上传准备");
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
//        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "wqunassets";
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        //从http://oos.wqun.top/img/covar/
        String split = oosImgPath.split("\\?")[0].substring(30);
        String objectName = "img/covar/" + split;
        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
        //String filePath= "D:\\test\\file\\img\\20230901102417a36169ff56ad40e28d6789fc5817e3ff.jpg";
        String accessKeyId = "LTAI5tR9m1d1hZAXeDzqKNxX";
        String accessKeySecret = "W3L3zbHjVAK81sqxMLNX2QPqNF06Te";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);

        try {
            // 创建PutObjectRequest对象。
            //PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, fileImg);
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 删除文件。
            ossClient.deleteObject(bucketName,objectName);
            return true;
        } catch (OSSException oe) {
//            System.out.println("Caught an OSSException, which means your request made it to OSS, "
//                    + "but was rejected with an error response for some reason.");
//            System.out.println("Error Message:" + oe.getErrorMessage());
//            System.out.println("Error Code:" + oe.getErrorCode());
//            System.out.println("Request ID:" + oe.getRequestId());
//            System.out.println("Host ID:" + oe.getHostId());
            return false;
        } catch (ClientException ce) {
//            System.out.println("Caught an ClientException, which means the client encountered "
//                    + "a serious internal problem while trying to communicate with OSS, "
//                    + "such as not being able to access the network.");
//            System.out.println("Error Message:" + ce.getMessage());
            return false;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}